// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "TABDMP1Parser.h"

#include <algorithm>
#include <cstdint>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Table/TABDMP1Data.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Table/ITABDMP1Service.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (TABDMP1);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/TABDMP1/TABDMP1.md>
*/
// 以下需要修改

SG::DataStructure::Common::Status BDF::BULKDATA::ParseTABDMP1 (const std::shared_ptr<DBServiceFactory>& dbServiceFactory,
                                                               std::vector<std::string>&                dividedEntryInfo)
{
    // 创建临时节点数据结构，默认值为0
    SG::DataStructure::FEM::TABDMP1Data tempTABDMP1;
    unsigned char                       factorType = 1;  // 1 G   2 CRIT 3 Q
    // TODO(bug 001) m_dominId的正确填写
    tempTABDMP1.m_domainId = 1;

    tempTABDMP1.m_id = convertToI64 (dividedEntryInfo[1]);

    // 更新GFactor
    boost::trim (dividedEntryInfo[2]);
    boost::to_upper (dividedEntryInfo[2]);
    if (dividedEntryInfo[2] == "CRIT")
    {
        factorType = 2;
    }
    if (dividedEntryInfo[2] == "Q")
    {
        factorType = 3;
    }

    const size_t rowNum = dividedEntryInfo.size () / 10;

    for (size_t curRow = 1; curRow < rowNum; curRow++)
    {
        for (size_t fieldsColumn = 1; fieldsColumn < 9; fieldsColumn += 2)
        {
            // 计算当前字段的索引
            const size_t curIndex  = curRow * 10 + fieldsColumn;
            std::string& curStrKey = dividedEntryInfo[curIndex];
            boost::trim (curStrKey);
            // 发现endt 跳出循环
            if (curStrKey[0] == 'E' || curStrKey[0] == 'e')
            {
                break;
            }
            switch (factorType)
            {
            case 1:  // G
                tempTABDMP1.m_DAMPList.push_back ({ convertToReal (curStrKey), convertToReal (dividedEntryInfo[curIndex + 1]) });
                break;
            case 2:  // CRIT
                tempTABDMP1.m_DAMPList.push_back ({ convertToReal (curStrKey), 2 * convertToReal (dividedEntryInfo[curIndex + 1]) });
                break;
            case 3:  // Q
                tempTABDMP1.m_DAMPList.push_back ({ convertToReal (curStrKey), 1 / convertToReal (dividedEntryInfo[curIndex + 1]) });
                break;
            default:
                break;
            }
        }
    }

    // 将解析完成的节点数据添加到数据库服务中
    return dbServiceFactory->get<ITABDMP1Service> ()->Add (tempTABDMP1);
}